from codesters.demo import Demo
demo = Demo()
class newDemo(Demo):
def create_continue_button(self):
text = "Continue"
background_sprite = codesters.Rectangle(-203, -219, 125, 62.5, "green", "black")
button = self.create_button(text, -197, -220, True, "white", "#07A7E0", 90, 200, "white")
self.stage.remove_sprite(button.background_sprite)
button.background_sprite = background_sprite
self.add_hover_opacity_element(button)
return button
demo = newDemo()
def space_bar_2():
highlight_box.set_y(195)
highlight_box.set_height(31)
highlight_box.show()
sprite.move_up(100)
highlight_box.set_y(165)
stage.wait(0.1)
highlight_box.set_y(135)
sprite.move_down(100)
highlight_box.hide()
def code_editor():
background1 = codesters.Rectangle(0, 167, 500, 165, "black")
left_guide_a = codesters.Line(-215, 250, -215, 84, "gray")
y = 225
spacing = 30
indent = 40
line1 = codesters.Text("def space_bar():", -138, y, "white")
y-= spacing
line2 = codesters.Text(".move_up( )", -90 + indent, y, "white")
line2a = codesters.Text('sprite', -145, y, "orange")
line2b = codesters.Text('100', -8, y, "dodgerblue")
indent1 = codesters.Rectangle(-192, y, indent, spacing, "darkolivegreen")
y-= spacing
line3 = codesters.Text("stage", -145, y, "orange")
line3a = codesters.Text(".wait( . )", -74, y, "white")
line3b = codesters.Text('0 1', -54, y, "dodgerblue")
indent2 = codesters.Rectangle(-192, y, 40, spacing, "darkolivegreen")
y-= spacing
line4 = codesters.Text("sprite", -145, y, "orange")
line4a = codesters.Text(".move_down( )", -36, y, "white")
line4b = codesters.Text('100', 20, y, "dodgerblue")
indent3 = codesters.Rectangle(-192, y, 40, spacing, "darkolivegreen")
y-=spacing
line5 = codesters.Text("stage", -145-indent, y, "orange")
line5a = codesters.Text(".event_key( , )", -12.5, y, "white")
line5b = codesters.Text('"space"', -20, y, "lightgreen")
line5c = codesters.Text("space_bar", 78, y, "orange")
code_list = [background1, left_guide_a, line1,
line2, line2a, line2b, indent1,
line3, line3a, line3b, indent2,
line4, line4a, line4b, indent3,
line5, line5a, line5b, line5c]
y_count = 225
for counter in range(3, 8):
n = codesters.Text(counter, -235, y_count, "grey")
n.set_size(.8)
y_count -= spacing
code_list.append(n)
for line in code_list:
line.hide()
return code_list
code_list = code_editor()
sprite = codesters.Sprite("superhero2")
def space_bar():
sprite.move_up(100)
stage.wait(.1)
sprite.move_down(100)
stage.event_key("space", space_bar)
explanation = codesters.Text("Press the spacebar on your keyboard,\nthen click Continue!", 0, -150)
stage.wait(2)
demo.continue_action()
sprite.glide_to(175, -35)
explanation.set_text("Why does pressing the spacebar make me jump?")
stage.wait(1)
demo.continue_action()
explanation.set_text("Let's check the code!")
sprite.go_to(175, -35)
stage.wait(1)
demo.continue_action()
for line in code_list:
line.show()
stage.wait(1)
highlight_box = codesters.Rectangle(-45, 103, 400, 31, "yellow")
highlight_box.set_opacity(.3)
sprite.go_to(175, -35)
explanation.set_text("The event handler listens for the signal to run the event. The signal is pressing the Space Bar key.")
stage.wait(2)
demo.continue_action()
highlight_box.go_to(-45, 225)
sprite.go_to(175, -40)
explanation.set_text("This line creates an event and names it space_bar.")
stage.wait(2)
demo.continue_action()
highlight_box.set_y(165)
highlight_box.set_height(93)
sprite.go_to(175, -35)
stage.event_key("space", None)
explanation.set_text("The indented commands run every time the event handler detects the signal.")
stage.wait(2)
explanation.set_text("\nThe indented commands run every time the event handler detects the signal.\nPress space!")
stage.event_key("space", space_bar_2)
stage.wait(4)
demo.continue_action()
sprite.go_to(175, -35)
highlight_box.set_y(105)
stage.event_key("space", space_bar)
explanation.set_text("Every time you press the spacebar, the space_bar() function runs.")
stage.wait(2)
demo.continue_action()
sprite.go_to(175, -35 )
code_list[2].hide()
stage.event_key("space", space_bar_2)
tester = TestManager()
tester.display_success_message("Great job! Click Submit and Next to move on.")